//
//  Weapon.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-4-26.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import "Weapon.h"
#import "EquipInfo.h"
#import "EquipInfoMgr.h"
#import "EquipModelInfo.h"
#import "EquipModelInfoMgr.h"
#import "BulletModelInfoMgr.h"
#import "BulletCache.h"
#import "BattleMainLayer.h"
#import "BattleScene.h"

@implementation Weapon

@synthesize equipId = _equipId;
@synthesize isTeam = _isTeam;
@synthesize view = _view;
@synthesize fsmManage = _fsmManage;

+(id) weaponWithId:(int) equipId
{
    return [[[self alloc] initWithId:equipId] autorelease]; 
}

-(id) initWithId:(int) equipId
{
    self = [super init]; 
	
	if (self!= nil) 
    {
        _equipId = equipId;
        //查装备表
        EquipInfo* equipInfo = [[EquipInfoMgr sharedManager] getInfoById:equipId];
        if(equipInfo)
        {
            //查装备模型表
            EquipModelInfo* equipModelInfo = [[EquipModelInfoMgr sharedManager] getInfoById:equipInfo.modelId];
            if(equipModelInfo)
            {
                //视图
                self.view = [AutomaticGunView viewWithModelId:equipInfo.modelId];
                
                [self addChild:self.view];
                
                self.rotation = 90;
                
                //状态机
                self.fsmManage = [WeaponFSMManager managerWithWeapon:self];
            }
            else
            {
                CCLOG(@"装备模型表查找失败：%d", equipInfo.modelId);
            }
        }
        else
        {
            CCLOG(@"装备表查找失败：%d", equipId);
        }
        
        updateCount = 0;
        [self scheduleUpdate];
    }
    
    return self;
}

-(void) update:(ccTime)delta 
{
    updateCount++;
    
    if(updateCount % 10 == 1)
    {
//        self.rotation = self.rotation + 1;
        [self doFire];
    }
}

/**
	 执行开火操作
 */
-(void) doFire
{
    //查装备模型表，再查子弹表
    EquipInfo* equipInfo = [[EquipInfoMgr sharedManager] getInfoById:_equipId];
    if(equipInfo)
    {
        EquipModelInfo* equipModelInfo = [[EquipModelInfoMgr sharedManager] getInfoById:equipInfo.modelId];
        if(equipModelInfo)
        {
            NSAssert([self.parent isKindOfClass:[Ship class]], @"not a Ship!");
            Ship* ship = (Ship*)self.parent;
            float newRotation = self.rotation + ship.rotation;
            CGPoint startP = ccpAdd(ship.position, self.position);
            [[[BattleScene sharedLayer] mainLayer].bulletCache shootBulletAt:startP rotation:newRotation speed:200 range:1000 damageType:1 damageValue:1000 bulletId:equipModelInfo.bulletId isTeam:_isTeam];
        }
        else
        {
            CCLOG(@"开火：装备模型表查找失败：%d", equipInfo.modelId);
        }
    }
    else
    {
        CCLOG(@"开火：装备表查找失败：%d", _equipId);
    }
}

- (void) dealloc
{
	if(_view != nil)//retain要release
    {
        [_view release];
        _view = nil;
    }
	
    if(_fsmManage != nil)
    {
        [_fsmManage release];
        _fsmManage = nil;
    }
    
	// don't forget to call "super dealloc"
	[super dealloc];
}

@end
